package com.apestech.framework.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.instruction.op.OperatorBase;

public class StringFuncUtil extends StringUtil {
	
	/**
	 * 根据子串，获取每节子串对应的位置子集
	 * @param text
	 * @param params 多个查找关键字合集
	 * @return
	 * @throws Exception 
	 */
	public static ArrayList<Integer> indexOfList(String text, String... params) throws Exception{
		String sub=text;
        ArrayList<Integer> list=new ArrayList<Integer>();
        JSONObject map=getVarPos(sub, params);
        int pos=map.getIntValue("pos");
        String keyword=map.getString("key");
        int lastpos=0;
        while (pos>=0) {
        	int length=sub.length();
        	sub=sub.substring(sub.indexOf(keyword)+keyword.length(), length);
        	list.add(pos+lastpos);
        	lastpos=lastpos+pos+keyword.length();
        	map=getVarPos(sub, params);
        	pos=map.getIntValue("pos");
        	keyword=map.getString("key");
        } 
        return list;
	}
	
	/**
	 * 执行表达式
	 * @param expression String    表达式
	 * @param params     Object... 表达式对应变量的参数集
	 * @return
	 * @throws Exception 
	 */
	public Object doExpression(String expression, Object... params) throws Exception{
		return doExpression(expression, true, params);
	}

	public Object doExpression(String expression, Boolean check, Object... params) throws Exception{
		Expression exp=new Expression(expression);
		exp.setParams(params);
		exp.setCheck(check);
		return exp.exec();
	}
	
	/**
	 * 获取存在多个关键字时字串的位置
	 * @param text
	 * @param params 值的先后顺序为判断的先后顺序
	 * @return
	 */
	public static JSONObject getVarPos(String text, String... params){
		int pos=-1;
		String key="";
		int ipos=-1;
		int len=params.length;
		String key1="";
		for(int i=0; i<len; i++){
			key=params[i];
			pos=text.indexOf(key);
			if(pos>=0){
				if(ipos==-1) {
					ipos=pos;
					key1=key;
				}
				else if(pos<ipos) {
					ipos=pos;
					key1=key;
				}
			}
		}
		JSONObject map= new JSONObject();
		map.put("pos", ipos);
		map.put("key", key1);
		return map;
	}
	
	/**
	 * 根据子串返回对应文本的明细结构数据
	 * @param startList
	 * @param keyword
	 * @param text
	 * @return
	 * @throws Exception
	 */
	public static JSONObject getMapping(ArrayList<Integer> startList, String keyword, String text) throws Exception{
		JSONArray list=new JSONArray();
		JSONObject rmap=new JSONObject();
		int slen=startList.size();
		if(slen==1){
			JSONObject map=new JSONObject();
			map.put("st", 0);
			map.put("en", text.length());
			map.put("text", text);
			rmap.put("index", 0);
			list.add(map);
		} else {
			for(int s=0; s<slen; s++){
				JSONObject map=new JSONObject();
				int istart=startList.get(s);
				int iend;
				if(s+1==slen){
					iend=text.length();
				} else {
					iend=startList.get(s+1);
				}
				String sub=text.substring(istart, iend);
				if(countString(sub, keyword)>1){
					rmap.put("index", list.size());
				}
				map.put("st", istart);
				map.put("en", iend);
				map.put("text", sub);
				list.add(map);
			}
		}
		
		rmap.put("list", list);
		return rmap;
	}

	/**
	 * 获取符合条件的格式化子串，拼SQL专用
	 * @param col 全部列表变量
	 * @param filter 存在特殊处理的子串名
	 * @param in 是否为包含关系
	 * @return
	 */
	public static String getCol(String col, String filter, boolean in){
		if(filter!=null){
			if(in){
				col=getSubStr(col, filter, ",", true);
			} else {
				col=getSubStr(col, filter, ",", false);
			}
		}
		return col;
	}

	/**
	 * 根据主键KEY字段生成对应WHERE条件子句
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String getKeyWhere(String key, boolean hasWhere, String col, String timeField, boolean isTime) throws Exception {
		String rsql="";
		if(hasWhere) rsql=" WHERE 1=1 ";
		if("".equals(key)) return rsql;
        col=col.replace(",",";");
        key=key.replace(",",";");
		String[] list=key.split(";");
		int len=list.length;
		for(int i=0; i<len; i++){
			String s=list[i];
			if(!"".equals(s)){
				if((";"+col+";").indexOf(";"+s+";")>=0){
					if(s.equals(timeField) && isTime){
						rsql=rsql+" AND GET_TIMESTAMP("+s+")=#{"+s+"_OLD}";
					} else  rsql=rsql+" AND "+s+"=#{"+s+"}";
				}
			}
		}
		return rsql;
	}

	public JSONObject addQueryCondition(String field, String operator, Object value){
		JSONObject map=new JSONObject();
		map.put("field", field);
		map.put("operator", operator);
		map.put("value", value);
		return map;
	}

}
